#ifndef UPTODATEVERSIONS_H
#define UPTODATEVERSIONS_H

#include "CoreFarmAPI.h"

///////////////////////////////////////////////////////////////
//////Up To Date Versions NODE AND METHODS//////////////////////
///////////////////////////////////////////////////////////////

typedef struct UpToDateVersionsNode{

	int serviceID;
	int serviceVerson;
	struct UpToDateVersionsNode* next;

}UpToDateVersionsNode;

UpToDateVersionsNode* UpToDateVersionsNodeConstructor(){

	UpToDateVersionsNode *thisNode;

	thisNode = (UpToDateVersionsNode*)malloc(sizeof(UpToDateVersionsNode));
	assertMem(thisNode);

	thisNode->serviceVerson = 0;
	thisNode->serviceID = 0;
	thisNode->next = NULL;

	return thisNode;
}

void upToDateVersionsNodeSetServiceID( UpToDateVersionsNode *thisNode, int _serviceID) { thisNode->serviceID =_serviceID ; }
void upToDateVersionsNodeSetServiceVersion( UpToDateVersionsNode *thisNode, int _serviceVersion) { thisNode->serviceVerson =_serviceVersion ; }
void upToDateVersionsNodeSetNext( UpToDateVersionsNode *thisNode, UpToDateVersionsNode *_next) { thisNode->next = _next ;}
int  upToDateVersionsNodeGetServiceID( UpToDateVersionsNode *thisNode) {return thisNode->serviceID ;}
int  upToDateVersionsNodeGetServiceVersion( UpToDateVersionsNode *thisNode) {return thisNode->serviceVerson ;}
UpToDateVersionsNode * upToDateVersionsNodeGetNext( UpToDateVersionsNode *thisNode) { return thisNode->next ;}

////////////////////////////////////////////////////////////////////////////////////
//////// Up To Date Database List structure and methods ////////////////////////////
////////////////////////////////////////////////////////////////////////////////////

typedef struct UpToDateVersionsDB{

	int size;
	UpToDateVersionsNode *head;

}UpToDateVersionsDB;

UpToDateVersionsDB *upToDateVersions_DB = NULL;

void upToDateVersionsDBConstructor(){
	upToDateVersions_DB = (UpToDateVersionsDB*)malloc(sizeof(UpToDateVersionsDB));
	assertMem(upToDateVersions_DB);

	upToDateVersions_DB->size = 0;
	upToDateVersions_DB->head = NULL;
}

void upToDateVersionsDBSetHead(UpToDateVersionsNode *_head){ upToDateVersions_DB->head = _head ;}
UpToDateVersionsNode* upToDateVersionsDBGetHead(){ return upToDateVersions_DB->head ;}
int upToDateVersionsDBIsEmpty(){ return (upToDateVersions_DB->size == 0) ;}

void upToDateVersionsDBPush( int _serviceID, int _serviceVerson){

	UpToDateVersionsNode *newUpToDateVersions = UpToDateVersionsNodeConstructor();

	upToDateVersionsNodeSetServiceID( newUpToDateVersions, _serviceID);
	upToDateVersionsNodeSetServiceVersion( newUpToDateVersions, _serviceVerson);

	if ( !upToDateVersions_DB )
		upToDateVersionsDBConstructor();

	if( !upToDateVersionsDBIsEmpty() )
		newUpToDateVersions->next = upToDateVersions_DB->head;

	upToDateVersions_DB->head = newUpToDateVersions;
	upToDateVersions_DB->size++;
}

int getUpToDateServiceVer(int serviceID){
	UpToDateVersionsNode *currNode = NULL;

	if( upToDateVersions_DB )
		currNode = upToDateVersions_DB->head;
	while( currNode )
	{
		if( currNode->serviceID == serviceID )
			return currNode->serviceVerson;
		currNode = currNode->next;
	}

	return 0;
}

UpToDateVersionsNode* findUpToDateVersionsNode(int serviceID){
	UpToDateVersionsNode *currNode = NULL;

	if( upToDateVersions_DB )
		currNode = upToDateVersions_DB->head;
	while( currNode )
	{
		if( currNode->serviceID == serviceID )
			return currNode;
		currNode = currNode->next;
	}

	return NULL;
}

void upToDateVersionsPrintAll(){
	UpToDateVersionsNode *currNode = NULL;

	if ( upToDateVersions_DB )
		currNode = upToDateVersions_DB->head;
	while( currNode ){
		printf("service ID: %d \nService Version: %d\n",currNode->serviceID, currNode->serviceVerson);
		currNode = currNode->next;
	}//while
}//Up To Date PrintALL

#endif